/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.user.client.ui;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.HasBlurHandlers;
import com.google.gwt.event.dom.client.HasFocusHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasValue;

/**
 * A focus group for binding pending-change-handler for a {@link HasValue} that compounds numbers of
 * input. The {@link ValueChangeEvent} should be fired when the focus out of this group, that means
 * when the focus transformed from one input to another in this group, the {@link ValueChangeEvent}
 * will never fired.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class FocusGroup<V> {

  /**
   * The focus handler for pending change.
   */
  private class InputFocusHandler implements FocusHandler, BlurHandler {
    private boolean pendingChange;

    @Override
    public void onBlur(BlurEvent event) {
      pendingChange = false;
      Scheduler.get().scheduleDeferred(new Command() {

        @Override
        public void execute() {
          if (!pendingChange) {
            V newValue = target.getValue();
            ValueChangeEvent.fireIfNotEqual(target, oldValue, newValue);
            oldValue = newValue;
          }
        }
      });
    }

    @Override
    public void onFocus(FocusEvent event) {
      if (!pendingChange) {
        pendingChange = true;
      }
    }
  }

  private InputFocusHandler inputFocusHandler;

  private V oldValue;

  private final HasValue<V> target;

  /**
   * Creates a focus group with the target that takes input({@link Focusable})s
   * 
   * @param valueProvider
   */
  public FocusGroup(HasValue<V> target) {
    this.target = target;
  }

  /**
   * Adds a focusable to this group.
   * 
   * @param focusable the focusable
   */
  public void addFocusable(Focusable focusable) {
    if (inputFocusHandler == null) {
      inputFocusHandler = new InputFocusHandler();
    }
    // fire change event if no pending.
    ((HasBlurHandlers) focusable).addBlurHandler(inputFocusHandler);

    // if focus in the input group, pend the value change event
    ((HasFocusHandlers) focusable).addFocusHandler(inputFocusHandler);
  }

}
